package servlets;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

/**
 * Servlet Filter implementation class HeadersFilter
 */
@WebFilter("/HeadersFilter")
public class HeadersFilter implements Filter {
	private FilterConfig fConfig;
    /**
     * Default constructor. 
     */
    public HeadersFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		
		Enumeration<String> headers = ((HttpServletRequest)request).getHeaderNames();
		while(headers.hasMoreElements()) {
			String name = headers.nextElement(); 
			String value = ((HttpServletRequest)request).getHeader(name);
			this.fConfig.getServletContext().log(name+": "+value);
			System.out.println(name+": "+value);
		}
		chain.doFilter(request, response);

	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		this.fConfig = fConfig;
	}

}
